//package com.punch.order.config;
//
//import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
//import com.punch.common.enums.order.OrderStatusEnum;
//import com.punch.common.feign.FeignClassService;
//import com.punch.common.feign.FeignUserService;
//import com.punch.common.utils.BigDecimalUtil;
//import com.punch.common.utils.IdGenerator;
//import com.punch.common.vo.course.CourseSettlementVO;
//import com.punch.common.vo.user.user.UserExtendVO;
//import com.punch.order.model.CourseSettlement;
//import com.punch.order.model.CourseSettlementDetail;
//import com.punch.order.model.Order;
//import com.punch.order.service.CourseSettlementDetailService;
//import com.punch.order.service.CourseSettlementService;
//import com.punch.order.service.OrderService;
//import com.punch.order.wxpay.constant.WXPayConstants;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.beans.BeanUtils;
//import org.springframework.scheduling.annotation.Scheduled;
//import org.springframework.stereotype.Component;
//import org.springframework.transaction.annotation.Transactional;
//
//import javax.annotation.Resource;
//import java.math.BigDecimal;
//import java.time.LocalDate;
//import java.util.ArrayList;
//import java.util.List;
//import java.util.Map;
//import java.util.stream.Collectors;
//
//@Component
//@Slf4j
//public class OrderTask {
//
//
//    @Resource
//    private OrderService orderService;
//
//    @Resource
//    private FeignClassService feignClassService;
//
//    @Resource
//    private CourseSettlementService courseSettlementService;
//
//    @Resource
//    private CourseSettlementDetailService courseSettlementDetailService;
//
//    @Resource
//    private FeignUserService feignUserService;
//
//
//    /**
//     * 未支付订单取消
//     */
//    @Scheduled(cron = "59 59 23 * * ?")
//    @Transactional(rollbackFor = RuntimeException.class)
//    public void cancelUnPayOrder() {
//        //查找所有未支付的订单
//        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
//        queryWrapper.lambda().eq(Order::getOrderStatus, OrderStatusEnum.TO_PAY.getCode());
//        List<Order> list = orderService.list(queryWrapper);
//        list.forEach(orderEX -> {
//            Order order = new Order();
//            order.setId(orderEX.getId());
//            order.setOrderNo(orderEX.getOrderNo());
//            order.setContainMaterialFlag(orderEX.getContainMaterialFlag());
//            order.setOrderStatus(OrderStatusEnum.HAVE_CANCEL.getCode());
//            orderService.updateOrderStatus(order, WXPayConstants.CANCEL_UNPAY_ORDER_REMARK, "");
//        });
//    }
//
//    /**
//     * 每月1号1点 结算上个月
//     */
////    @Scheduled(cron = "0 0/1 * * * ?")
//    @Scheduled(cron = "0 0 1 1 * ?")
//    @Transactional(rollbackFor = RuntimeException.class)
//    public void settlement() {
//        //查找所有未结算完成的课程
//        List<CourseSettlementVO> courseSettlementList = feignClassService.listCourseSettlement();
//        Map<Long, List<CourseSettlementVO>> courseCreateMap = courseSettlementList.stream().collect(Collectors.groupingBy(CourseSettlementVO::getCourseCreateId));
//        //机构/学校 统计课程结算
//        List<CourseSettlement> list = new ArrayList<>();
//        //班级课程结算信息
//        List<CourseSettlementDetail> listDetail = new ArrayList<>();
//        LocalDate preMonthDay = LocalDate.now().minusMonths(1);
//        courseCreateMap.forEach((courseCreateId, v) -> {
//            CourseSettlement courseSettlement = new CourseSettlement();
//            Long settlementId = IdGenerator.getId();
//            courseSettlement.setId(settlementId);
//            CourseSettlementVO firstCourse = v.get(0);
//            courseSettlement.setSettlementToId(courseCreateId);
//            courseSettlement.setSettlementToName(firstCourse.getCourseCreateName());
//            //查找学校结算信息
//            UserExtendVO userExtendVO;
//            if (firstCourse.getOrganId() == null || firstCourse.getOrganId() == 0L) {
//                courseSettlement.setSettlementType(0);
//                userExtendVO = feignUserService.getUserSettlement(firstCourse.getCourseCreateId(), 0).getData();
//            } else {
//                //查找机构结算信息
//                courseSettlement.setSettlementType(1);
//                userExtendVO = feignUserService.getUserSettlement(firstCourse.getCourseCreateId(), 1).getData();
//            }
//            //计算该机构/学校下上个月班级开课情况
//            CourseSettlementDetail courseSettlementDetail;
//            BigDecimal orgSettlementAmount = new BigDecimal(0);
//            BigDecimal settlementAmount;
//            for (CourseSettlementVO courseSettlementVO : v) {
//                courseSettlementDetail = new CourseSettlementDetail();
//                BeanUtils.copyProperties(courseSettlementVO, courseSettlementDetail);
//                courseSettlementDetail.setSettlementId(settlementId);
//                courseSettlementDetail.setCourseClassId(courseSettlementVO.getClassId());
//                courseSettlementDetail.setCourseClassName(courseSettlementVO.getClassName());
//                courseSettlementDetail.setSchoolId(courseSettlementVO.getCreateId());
//                courseSettlementDetail.setSchoolName(courseSettlementVO.getCreateName());
//                //计算该班级课程的结算金额
//                settlementAmount = calculateClassSettlementAmount(courseSettlementVO, userExtendVO);
//                courseSettlementDetail.setSettlementAmount(settlementAmount);
//                listDetail.add(courseSettlementDetail);
//                orgSettlementAmount = BigDecimalUtil.plus(orgSettlementAmount, settlementAmount);
//            }
//            courseSettlement.setSettlementAmount(orgSettlementAmount);
//            courseSettlement.setSettlementMonth(preMonthDay);
//            list.add(courseSettlement);
//        });
//        courseSettlementService.saveBatch(list);
//        courseSettlementDetailService.saveBatch(listDetail);
//    }
//
//    /**
//     * 计算班级结算金额
//     */
//    private BigDecimal calculateClassSettlementAmount(CourseSettlementVO courseSettlementVO, UserExtendVO userExtendVO) {
//        Integer signUpNumber = courseSettlementVO.getSignUpNumber();
//        Integer settlementClassCount = courseSettlementVO.getSettlementClassCount();
//        BigDecimal settlementAmount = new BigDecimal(0);
//        //结算方式 0-固定
//        Integer settlementType = userExtendVO.getSettlementType();
//        if (settlementType == 0) {
//            BigDecimal settlementMoney = userExtendVO.getSettlementMoney();//一节课固定抽多少钱
//            BigDecimal sub = BigDecimalUtil.sub(courseSettlementVO.getCourseHourPrice(), settlementMoney);
//            settlementAmount = BigDecimalUtil.mul(BigDecimalUtil.mul(sub, signUpNumber), settlementClassCount);
//        }
//        //结算方式  1-比例
//        if (settlementType == 1) {
//            //课程单价*报名人数*本月已经上课次数
//            BigDecimal courseHourPrice = courseSettlementVO.getCourseHourPrice();
//            BigDecimal totalMoney = BigDecimalUtil.mul(BigDecimalUtil.mul(courseHourPrice, signUpNumber), settlementClassCount);
//            log.info("{}的总金额为:{}", courseSettlementVO.getCourseName(), totalMoney);
//            //按照阿里规约 double会存在精度丢失问题,建议传入String oop规约12,比例是百分比,需要除以100
//            BigDecimal settlementRatioPercent = BigDecimalUtil.div(String.valueOf(userExtendVO.getSettlementRatio()), "100");
//            BigDecimal sub = BigDecimalUtil.sub(new BigDecimal(1), settlementRatioPercent);
//            settlementAmount = BigDecimalUtil.mul(totalMoney, sub);
//        }
//        log.info("结算方式:比例【课程:{}的班级:{}统计总金额{}】", courseSettlementVO.getCourseName(), courseSettlementVO.getClassName(), settlementAmount);
//        return settlementAmount;
//    }
//}
